package cn.edu.controller;

import cn.edu.pojo.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

/**
 * @PackageName: cn.edu.controller
 * @ClassName: ConsumerController
 * @Description:
 * @author: RZS
 * @date: 2021/6/18  11:13
 */
@Api(tags = "服务消费者-ConsumerController")
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String PATH_URL = "http://localhost:8010/student";

    @ApiOperation("查询所有student数据接口")
    @GetMapping("/find_all")
    public Collection<Student> findAll(){
        // restTemplate.getForObject(PATH_URL+"/find_all", Collection.class);
        return restTemplate.getForEntity(PATH_URL+"/find_all", Collection.class).getBody();
    }

    @ApiOperation("根据id查询student")
    @ApiImplicitParam(name = "id", value = "id")
    @GetMapping("/find_by_id/{id}")
    public Student findnamById(@PathVariable("id")Integer id){
        //restTemplate.getForEntity(PATH_URL+"/find_by_id/{id}", Student.class, id).getBody();
        return restTemplate.getForObject(PATH_URL+"/find_by_id/"+id, Student.class);
    }

    @ApiOperation("添加student数据接口")
    @ApiImplicitParam(name = "student", value = "student对象")
    @PostMapping("/add")
    public void add(@RequestBody Student student){
        restTemplate.postForEntity(PATH_URL+"/add", student, Student.class);
    }

    @ApiOperation("根据id删除student对象")
    @ApiImplicitParam(name = "id", value = "id")
    @DeleteMapping("/{id}")
    public void removeById(@PathVariable("id")Integer id){
        restTemplate.delete(PATH_URL+"/{id}", id);
    }

    @ApiOperation("修改student数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student", value = "student对象"),
            @ApiImplicitParam(name = "id", value = "id")
    })
    @PutMapping("/update/{id}")
    public void changeById(@RequestBody Student student, @PathVariable("id") Integer id){
        restTemplate.put(PATH_URL+"/update/{id}", student, id);
    }
}
